const app = getApp()
var request = require('../../utils/request.js');
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var amapInstance;
Page({
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    detid:'',
    lat:'',
    lon:'',
    deta:'',
    image: 'https://zgg.chuangkegf.com',
    phone:''
  },
  onLoad: function (options) {
    // console.log(options)
    var that = this;


    amapInstance = new QQMapWX({
      key: 'L6FBZ-YQ4CG-URYQY-IH36I-SXRFZ-IEBI5' //此处使用你自己申请的key
    })
    // 获取果园详情
    that.setData({
      detid: options.id,
      lat: options.lat,
      lon: options.lon
    });
    that.getData();
  },
  // 获取果园详情
  getData: function () {
    var that = this;
    var url = request.dataUrl + '/Orchard/detail';
    var params = {
      id: that.data.detid,
      latitude: that.data.lat,
      longitude: that.data.lon
    }
    request.requestLoading(url, params, '正在加载数据', function (res) {
      //res就是我们请求接口返回的数据
      console.log(res)
      wx.setNavigationBarTitle({
        title: res.data.name
      })
      that.setData({
        deta: res.data,
        list:res.data.cat,
        pinglun: res.data.comment,
        phone: res.data.phone,
        lunbo: res.data.banner,
        chanpin: res.data.img_url
      });
    }, function (res) {
      wx.showToast({
        title: '加载数据失败',
        image:'../../image/fail.png'
      })
    })
  },
  // 评论跳转
  pinglun:function(e){
    // console.log(e.currentTarget.dataset.id)
    var cid = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../comment/comment?cid=' + cid
    })
  },
  // 拨打电话
  phone:function(e){
    var that = this
    wx.makePhoneCall({
      phoneNumber: that.data.phone //仅为示例，并非真实的电话号码
    })
  },
  // 查看更多评论
  gengduo:function(e){
    console.log(e)
    var cid = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../gengduo/gengduo?cid=' + cid
    })
  },
  // 距离
  openmap: function (e) {
    // console.log(e)
    var add = e.currentTarget.dataset.add;
    var name = e.currentTarget.dataset.name;

    // 调用腾讯接口
    amapInstance.geocoder({
      address: add + name,
      success: function (res) {
        // console.log(res);

        var latitude = res.result.location.lat;
        var longitude = res.result.location.lng;
        //  console.log(latitude, longitude);

        wx.getLocation({
          type: 'wgs84',
          success: function (res) {
            var latnum = Number(latitude);
            var longnum = Number(longitude);
            wx.openLocation({
              latitude: latnum,
              longitude: longnum,
              scale: 28,
              name: name
            })
          }
        })

      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });



  },
  changeIndicatorDots: function (e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function (e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function (e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function (e) {
    this.setData({
      duration: e.detail.value
    })
  },
  onShow: function () {
      var that = this
      that.getData();
  },
})